﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MaximPortal.Model;
using MaximServices.Model;
using MaximServices.Services;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace maxim_portal.Controllers
{
    [EnableCors("AllowCors")]
    [Route("api/[controller]")]
    [ApiController]
    public class MottoController : ControllerBase
    {
        private MottoServices _mottoServices;

        public MottoController(MottoServices mottoServices)
        {
            _mottoServices = mottoServices;
        }

        // GET: api/Motto
        [HttpGet]
        public ResultModel<List<Motto>> Get([FromQuery]string keyword, string status = "")
        {
            try
            {
                var list = _mottoServices.GetList(keyword, status);

                return BuildResult.Success<List<Motto>>(list);
            }
            catch (Exception e)
            {
                return BuildResult.Error<List<Motto>>(e.Message, null);
            }
        }

        // POST: api/Motto
        [HttpPost]
        public ResultModel<CreateMottoResultModel> Post([FromBody] AddMottoModel mottoList)
        {
            if (!ModelState.IsValid)
            {
                return BuildResult.Error(ModelState.Values.First().Errors[0].ErrorMessage, default(CreateMottoResultModel));
            }
            CreateMottoResultModel datalist = new CreateMottoResultModel();

            try
            {
                mottoList.list.ForEach(c =>
                    {
                        datalist.DataList.Add(new CreateMottoDataModel
                        {
                            auth = c.auth_name,
                            content = c.motto_content,
                            isSuccess = true
                        });
                    });
                return _mottoServices.BatchCreateMotto(datalist);
            }
            catch (Exception e)
            {
                return BuildResult.Error(e.Message, default(CreateMottoResultModel));
            }
        }
    }
}
